package com.support.android.designlibdemo.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.badoo.mobile.util.WeakHandler;
import com.pnikosis.materialishprogress.ProgressWheel;
import com.support.android.designlibdemo.R;
import com.support.android.designlibdemo.adapter.TitleListAdapter;
import com.support.android.designlibdemo.model.Datautil;
import com.support.android.designlibdemo.model.TitleListModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2015/11/16.
 */
public class CartoonListFragment extends Fragment implements TitleListAdapter.titleCallBack {

    @Bind(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @Bind(R.id.swipe)
    SwipeRefreshLayout mSwipeRefresh;
    @Bind(R.id.progress_wheel)
    ProgressWheel mWheel;
    private List<TitleListModel> mData;
    private TitleListAdapter mAdapter;
    private int pageNum;

    public static CartoonListFragment getInstanse() {
        return new CartoonListFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.title_list_layout, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
       // mWheel.setProgress(0);
        setRefresh();
        setupAdapter();
        testLoad();

    }

    private void setRefresh() {
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                testLoad();
               // setProgress(true);
            }
        });
        mSwipeRefresh.setRefreshing(true);
    }

    private void setupAdapter() {
        mData = new ArrayList<TitleListModel>();
        mAdapter = new TitleListAdapter(getActivity(), mData);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

    }

    @Override
    public void OnSingleClick() {
        /*^_^*/
    }

    private void testLoad() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<TitleListModel> list = Datautil.ParseTitleData(Datautil.PIC_TYPE_CARTOON, pageNum);
                Message message = new Message();
                message.obj = list;
                message.what = 0x11;
                mWearHand.sendMessageDelayed(message, 2000);

            }
        }).start();

    }

    private WeakHandler mWearHand = new WeakHandler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            List<TitleListModel> data = (List<TitleListModel>) message.obj;
            mData.addAll(data);
            pageNum++;
            mSwipeRefresh.setRefreshing(false);
          //  setProgress(false);
            mAdapter.notifyDataSetChanged();
            return false;
        }
    });

private void setProgress(boolean show)
{
    if(show)
    {
        mWheel.setVisibility(View.VISIBLE);
    }
    else
    {
        mWheel.setVisibility(View.GONE);
    }

}
    @Override
    public void onDestroy() {
        super.onDestroy();

        mWearHand.removeCallbacksAndMessages(null);
    }
}


